<?php

namespace app\controller;

use app\BaseController;
use utils\RedisQueue;
class Miniprogram extends BaseController
{
    private $devTool;
    private $queue;
    private $versionList = [
        'merchant' => '商家端',
        'share' => '共享',
        'wechatopen' => '模板（开放平台）',
        'independent' => '独立版'
    ];
    function initialize(){
        parent::initialize();
        $this->devTool = new \utils\wechat\DevTool($this->version,$this->entoken);
        $this->queue = new RedisQueue($this->apiUrl,$this->sid);
    }
    public function login()
    {
        $rank = $this->queue->getRank();
        if ( $rank == -1 ){
            $this->queue->set(['time' => time()]);
            $rank = $this->queue->getRank();
        }
        //排队
        if ( $rank > 0 ){
            return $this->error("您前方还有{$rank}人等待上传，请稍等。如果轮到您了，请在5分钟之内上传完毕，否则过号重排。您无需关闭页面，请继续等待。");
        }
        $queue = $this->queue->get();
        if ( !$queue ){
             return $this->error("排队信息错误");
        }
        if ( $queue['sid'] != $this->sid ){
             return $this->error("排队信息错误");
        }
        $timeout = config('app.queueTimeout') - (time() - $queue['time']);
        if ( $timeout <= 0 ){
            $this->queue->finish();
            return $this->error("您在".config('app.queueTimeout').'秒内未扫码，已经将您移除队列，请您重新排队1',-1);
        }
        $data = $this->devTool->login();
    
        if ( $data ){
             return $this->success('OK',['qrcode' => $data,'resttime' => $timeout ]);
        }
        return $this->error("获取登录二维码失败");
       
    }
    public function cancel(){
        $ret = $this->queue->finish();
        if( $ret ){
            return $this->success('取消排队成功');
        }
        return $this->error('取消排队失败');
    }
    public function isLogin(){
        $queue = $this->queue->get();
        if ( $queue ){
            if ( $queue['sid'] != $this->sid ){
                return $this->error("排队信息错误");
            }
            $timeout = config('app.queueTimeout') - (time() - $queue['time']);
            if ( $timeout <= 0 ){
                 $list = $this->queue->getRank();
                 //$this->queue->remove($list);
                return $this->error("您在".config('app.queueTimeout').'秒内未扫码，已经将您移除队列，请您重新排队','',-1);
            }
        }
        
        $ret = $this->devTool->isLogin();
         return $this->success('OK',$ret);
    }
    public function getQueue(){
        $list = $this->queue->getRank();
        
        if ( $list < 0 ){
            $str = '队列为空';
        }else{
            $str = "您前方还有{$list}人等待上传，请稍等。如果轮到您了，请在5分钟之内上传完毕，否则过号重排。您无需关闭页面，请继续等待。";
        }
         return $this->success('OK',['order' => $str,'num' => $list]);
    }
    public function getData(){
         $list = $this->queue->getAll();
         print_r($list);
    }
    public function remove(){
        
    }
    public function preview()
    {
        echo $this->devTool->preview();
    }
    public function upload(){

        if( !isset($this->versionList[$this->version])){
            return $this->error("版本不正确");
        }
        $list = $this->queue->getRank();
                
        if ( $list == -1 ){
            //return $this->error("请先排队".$list);
        } 
        if ( $list != 0 ){
           // return $this->error("还未轮到您");
        } 
        $ret = $this->devTool->upload();
        
        if ( isset($ret['code']) ){
            if ( $ret['code'] == 10  ){
                return $this->error($ret['message'].'111','',$ret['code']);
            }
            if ( $ret['code'] == 0  ){
                
                return $this->error($ret['data'],'',$ret['code']);
            }
        }else{
             $this->queue->finish();
            return $this->success($ret);
        }
        
    }
}
